package outils;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import modele.Cardinalite;
import modele.ModeleClasse;
import modele.Relations;
import modele.Visibilite;


public class Divers 
{
	public static String EXTENSION = ".kck";
	public static String [] cardinalites = {"Choix","*","1..n","0..1","1"};
	
	public static Visibilite stringToVisib(String vis)
	{
		if(vis.equalsIgnoreCase("public"))
		{
			return Visibilite.PUBLIC;
		}
		else if (vis.equalsIgnoreCase("private"))
		{
			return Visibilite.PRIVATE;
		}
		else 
			return  Visibilite.PROTECTED;
	}
	
	public static Relations stringToRelation(String rel)
	{
		if(rel.equalsIgnoreCase("association"))
		{
			return Relations.ASSOCIATION;
		}
		else if(rel.equalsIgnoreCase("composition")) 
			return Relations.COMPOSITION;
		else
			return Relations.AGREGATION;
	}
	
	public static Cardinalite stringToCard(String card)
	{
		if(card.equalsIgnoreCase("1..n"))
			return Cardinalite.UN_N;
		else if(card.equalsIgnoreCase("*")) 
			return Cardinalite.ETOILE;
		else if(card.equalsIgnoreCase("1")) 
			return Cardinalite.UN;
		else
			return Cardinalite.ZERO_UN;
	}

	public static List<Element> classesFromXml(String fichier) throws JDOMException, IOException
	{
		org.jdom.Document doc;
		Element root;
		SAXBuilder bld = new SAXBuilder();
	    doc = bld.build(new File(fichier));
	    root = doc.getRootElement();
	    List<Element> listClasses = root.getChildren("classe");
	    return listClasses;
	}
	
	public static Vector<ModeleClasse> chargement(List<Element> listClasses)
	{
		Vector<ModeleClasse> liste = new Vector<ModeleClasse>();
		Iterator<Element> it = listClasses.iterator();
		//on remplit le vector de modelclasse à partir de la liste de classe
		while(it.hasNext())
		{
			Element classeCourante = it.next();
			liste.add(new ModeleClasse(classeCourante));
		}
		return liste;
	}
	
	
}